<?php

/*
	Class: Communitas_Integrate_Model_Catalog_Product_Attribute_Api

	About: Author
		Tyler Vigeant, Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Communitas_Integrate_Model_Catalog_Product_Attribute_Api extends Mage_Catalog_Model_Product_Attribute_Api {

	public function __construct() {
		parent::__construct();
	}

	public function items($setId) {
		$attributes = Mage::getModel('catalog/product')->getResource()
			->loadAllAttributes()
			->getSortedAttributes($setId);
		$result = array();
		foreach ($attributes as $attribute) {
			/* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
			if ((!$attribute->getId() || $attribute->isInSet($setId)) && $this->_isAllowedAttribute($attribute)) {
				if (!$attribute->getId() || $attribute->isScopeGlobal()) {
					$scope = 'global';
				} elseif ($attribute->isScopeWebsite()) {
					$scope = 'website';
				} else {
					$scope = 'store';
				}
				$result[] = array(
					'attribute_id' 		=> $attribute->getId(),
					'label'				=> $attribute->getFrontendLabel(),
					'code'		 		=> $attribute->getAttributeCode(),
					'type'		 		=> $attribute->getFrontendInput(),
					'required'	 		=> $attribute->getIsRequired(),
					'apply_to'	 		=> $attribute->getApplyTo(),
					'scope'				=> $scope,
					'is_visible'		=> $attribute->getIsVisible(),
					'is_configurable'	=> $attribute->getIsConfigurable(),
					'is_filterable'		=> $attribute->getIsFilterable(),
					'grouping'			=> $attribute->getAttributeSetInfo()
				);
			}
		}
		return $result;
	}

	public function groups($setId) {
		$groups = Mage::getModel('eav/entity_attribute_group')
			->getResourceCollection()
			->setAttributeSetFilter($setId)
			->load();
		return $groups->getData();
	}

	public function options($attributeId, $store = null) {

		return parent::options($attributeId, $store);

		$attribute = Mage::getModel('catalog/entity_attribute');
		$attribute
			->setStoreId($this->_getStoreId($store))
			->load($attributeId);

		/* @var $attribute Mage_Catalog_Model_Entity_Attribute */
		if (!$attribute->getId()) {
			$this->_fault('not_exists');
		}
		$options = array();
		if ($attribute->usesSource()) {
			$options = Mage::getResourceModel('eav/entity_attribute_option_collection')
				->setAttributeFilter($attribute->getId())
				->setStoreFilter()
				->load()
				->toOptionArray();
		}
		return $options;
	}

	public function loadByCode($code) {
		$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $code);
		// $attribute->label = $attribute->getFrontendLabel();
		return $attribute;
	}

}
